home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / CBCREATE.C < prev    next >
Text File  |  1991-09-23  |  4KB  |  157 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbcreate.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDDEF
  11. #include <stddef.h>
  12. #endif
  13. #include <stdio.h>
  14.  
  15. /* library headers */
  16. #include <blkio.h>
  17. #include <btree.h>
  18. #include <lseq.h>
  19.  
  20. /* local headers */
  21. #include "cbase_.h"
  22.  
  23. /* btree field definition list */
  24. static btfield_t btfldv[] = {
  25.     {
  26.         0,
  27.         0,
  28.         NULL,
  29.         BT_FASC
  30.     },
  31.     {
  32.         0,
  33.         sizeof(cbrpos_t),
  34.         cbrposcmp,
  35.         BT_FASC
  36.     },
  37. };
  38.  
  39. /*man---------------------------------------------------------------------------
  40. NAME
  41.      cbcreate - create a cbase
  42.  
  43. SYNOPSIS
  44.      #include <cbase.h>
  45.  
  46.      int cbcreate(cbname, recsize, fldc, fldv)
  47.      const char *cbname;
  48.      size_t recsize;
  49.      int fldc;
  50.      const cbfield_t fldv[];
  51.  
  52. DESCRIPTION
  53.      The cbcreate function creates a cbase.  cbname points to a
  54.      character string that contains the name of the cbase to be
  55.      created.  cbname is used as the name of the data file containing
  56.      the records in the cbase.
  57.  
  58.      recsize specifies the size of the records in the cbase.
  59.  
  60.      fldc is the field count.  It specifies the number of fields in
  61.      the records stored in this cbase.  fldv is an array of field
  62.      definition structures.  fldv must have fldc elements.  The field
  63.      definition structure is defined in <cbase.h> as type cbfield_t.
  64.      It has the following members.
  65.  
  66.           size_t offset;      /* offset of field in record *\/
  67.           size_t len;         /* field length *\/
  68.           int type;           /* field data type *\/
  69.           int flags;          /* flags *\/
  70.           char *filename;     /* name of key file *\/
  71.  
  72.      offset and len specify the location and length of the field,
  73.      respectively.  type is the data type for the field; see cbase
  74.      manual entry for a list of the predefined data types, and
  75.      the cbase Programmer's Guide for information on adding new data
  76.      types.  filename is the name of the file to be used for key
  77.      storage.  flags values are constructed by bitwise OR-ing flags
  78.      from the following list:
  79.  
  80.      CB_FKEY        Field is a key.
  81.      CB_FUNIQ       Only for use with CB_FKEY.  Indicates
  82.                     that the keys must be unique.
  83.  
  84.      The fields in the field definition list must be in order,
  85.      starting with the first field in the record.
  86.  
  87.      cbcreate will fail if one or more of the following is true:
  88.  
  89.      [EEXIST]       Either the record file or one of
  90.                     the key files exists.
  91.      [EINVAL]       cbname is the NULL pointer.
  92.      [EINVAL]       recsize is less than sizeof(cbrpos_t).
  93.      [EINVAL]       fldc is less than 1.
  94.      [EINVAL]       fldv is the NULL pointer.
  95.      [EINVAL]       fldv  contains an invalid field
  96.                     definition.
  97.  
  98. SEE ALSO
  99.      cbopen.
  100.  
  101. DIAGNOSTICS
  102.      Upon successful completion, a value of 0 is returned.  Otherwise,
  103.      a value of -1 is returned, and errno set to indicate the error.
  104.  
  105. ------------------------------------------------------------------------------*/
  106. #ifdef AC_PROTO
  107. int cbcreate(const char *cbname, size_t recsize, int fldc, const cbfield_t fldv[])
  108. #else
  109. int cbcreate(cbname, recsize, fldc, fldv)
  110. const char *cbname;
  111. size_t recsize;
  112. int fldc;
  113. const cbfield_t fldv[];
  114. #endif
  115. {
  116.     int    terrno    = 0;
  117.     int    i    = 0;
  118.  
  119.     /* validate arguments */
  120.     if (cbname == NULL || recsize < sizeof(cbrpos_t)) {
  121.         errno = EINVAL;
  122.         return -1;
  123.     }
  124.     if (!cb_fvalid(recsize, fldc, fldv)) {
  125.         errno = EINVAL;
  126.         return -1;
  127.     }
  128.  
  129.     /* create data file */
  130.     if (lscreate(cbname, recsize) == -1) {
  131.         if (errno != EEXIST) CBEPRINT;
  132.         return -1;
  133.     }
  134.  
  135.     /* create key files */
  136.     for (i = 0; i < fldc; ++i) {
  137.         if (fldv[i].flags & CB_FKEY) {
  138.             btfldv[1].offset = btfldv[0].len = fldv[i].len;
  139.             btfldv[0].cmp = cbcmpv[fldv[i].type];
  140.             if (btcreate(fldv[i].filename, CBM, fldv[i].len + sizeof(cbrpos_t), 2, btfldv) == -1) {
  141.                 if (errno != EEXIST) CBEPRINT;
  142.                 terrno = errno;
  143.                 for (i--; i >= 0; i--) {    /* remove files */
  144.                     if (fldv[i].flags & CB_FKEY) {
  145.                         remove(fldv[i].filename);
  146.                     }
  147.                 }
  148.                 remove(cbname);
  149.                 errno = terrno;
  150.                 return -1;
  151.             }
  152.         }
  153.     }
  154.  
  155.     return 0;
  156. }
  157.